<template>
	<view>
		<view class="header ">
			<view class="user_img">
				<image :src="avt" mode=""></image>
			</view>
			<view v-if="!musername==''" class="my_user">
				{{musername}}
			</view>
			<button v-else @tap="authUserInfo" type="default">登录</button>
		</view>
		<view class="main">
			<navigator class="user_list qcfd" url="../receiveAddres/receiveAddres">
				<view class="qcfd xhx">
					<view class="icon fl">
						<image style="width: 34rpx;height: 30rpx;margin-top: 4rpx;" src="/static/mine/kehuguanli.png" mode=""></image>
					</view>
					<view class="fl" style="margin-top: -2rpx;">
						客户信息管理
					</view>
					<view class="jiant fr">
						<image src="/static/mine/jiantou@2x.png"   ></image>
					</view>
				</view>

			</navigator>
			<navigator class="user_list qcfd" url="../fankui/fankui">
				<view class=" xhx qcfd">
					<view class="icon fl">
						<image style="width: 32rpx;height: 32rpx;" src="/static/mine/fankui.png" mode=""></image>
					</view>
					<view class="fl" style="margin-top: -2rpx;">
						用户反馈
					</view>
					<view class="jiant fr">
						<image src="/static/mine/jiantou@2x.png"   ></image>
					</view>
				</view>
			</navigator>
			<navigator class="user_list qcfd" url="../sheZhi/sheZhi">
				<view class=" xhx qcfd">
					<view class="icon fl">
						<image style="width: 34rpx;height: 34rpx;" src="/static/mine/shezhi.png" mode=""></image>
					</view>
					<view class="fl" style="margin-top: -2px;">
						设置
					</view>
					<view class="jiant fr">
						<image src="/static/mine/jiantou@2x.png"  ></image>
					</view>
				</view>
			</navigator>
		</view>
		<!-- 	 <button @click="abc">允许授权登录</button>
		<button @click="cba">允许授权登录</button>
		<button @click="cnm">登录</button>
		  <map style="width: 100%; height: 300px;" :latitude="latitude" :longitude="longitude" :markers="covers">  </map> -->
	</view>
</template>

<script>
	import {
		mapMutations
	} from "vuex";
	export default {
		data() {
			return {
				musername: "",
				avt: '../../static/mine/mine.png',
				id: 0, // 使用 marker点击事件 需要填写id
				title: "map",
				latitude: 39.909,
				longitude: 116.39742,
				covers: [{
					latitude: 39.909,
					longitude: 116.39742,
					iconPath: "../../static/logo.png",
				}, ],
			};
		},
		onShow() {
			if (uni.getStorageSync("token") && uni.getStorageSync("userInfo")) {
				this.musername = uni.getStorageSync("userInfo").phone;
				this.avt = uni.getStorageSync("userInfo").avatar;
				
			}
		},
		methods: {
			...mapMutations(["login", "logout"]),
			async wxlogin() {
				let res = await uni.login({
					provider: "weixin",
				});
				
				if (res[1] && res[1]["errMsg"] == "login:ok" && res[1]["code"]) {
					let jscode = res[1]["code"];
					uni.setStorageSync("sessionCode", jscode);
					
					let res1 = await this.$request.get(
						this.$requestUrl.JSCONDE2_SESSION_URL, {
							data: {
								code: jscode,
							},
						}
					);
					
					if (res1 && res1.token && res1.userInfo) {
						this.login(res1);
						this.musername = uni.getStorageSync("userInfo").phone;
						this.avt = uni.getStorageSync("userInfo").avatar;
						
					} else if (res1 && res1.session3rdKey) {
						uni.setStorageSync("session3rdKey", res1.session3rdKey);
						uni.setStorageSync("openid", res1.openid);
					} else if(res1 && res1.openid){
						uni.setStorageSync("openid", res1.openid);
						uni.navigateTo({
							url: "../login/login",
						});
					}
				} else {
					 this.$api.msg("获取登录参数失败,请重新获取！"); 
				}
			},
			async authUserInfo() {
				let res = await uni.getUserProfile({
					desc: "用户登录",
					lang: "zh_CN",
				});
				
				if (res && res[1] && res[1]["errMsg"] == "getUserProfile:ok") {
					await this.getUserInfo(
						res[1]["rawData"],
						res[1]["encryptedData"],
						res[1]["iv"],
						res[1]["signature"]
					);
				} else {
					this.$api.msg("获取用户信息失败,请重新获取！");
				}
			},
			async getUserInfo(rawData, encryptedData, iv, signature) {
				let res = await this.$request.get(this.$requestUrl.GET_USERINFO_URL, {
					data: {
						rawData: rawData,
						encryptedData: encryptedData,
						iv: iv,
						signature: signature,
						session3rdKey: uni.getStorageSync("session3rdKey"),
					},
				});
				
				if (res && res.openid) {
					//TODO跳转到登录页面
					uni.navigateTo({
						url: "../login/login",
					});
				} else {
					this.wxlogin();
					// this.$api.msg("获取用户信息失败,请重新获取！");
					console.log('kkkkkkkkkkkkkkkk')
				}
			},
			
		},
	};
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.header {
		padding: 40rpx;
	}

	.user_img {
		height: 128rpx;
		width: 128rpx;
		margin: 0 auto;
	}

	.user_img image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.header button {
		margin-top: 16rpx;
		width: 146rpx;
		height: 58rpx;
		font-size: 26rpx;
		border-radius: 31rpx;
		border: 2rpx solid #acacac;
		line-height: 58rpx !important;
		color: #7b7b7b;
	}

	.main {
		border-top: 16rpx solid #e9e9e9;
	}

	.user_list {
		padding: 0 32rpx;
	}

	.jiant image {
		width: 24rpx;
		height: 40rpx;
	}

	.user_list .fl {
		font-size: 28rpx;
		font-weight: 400;
		color: #181818;
		margin-right: 16rpx;
	}

	button::after {
		border: none;
	}

	.my_user {
		text-align: center;
		font-size: 36rpx;
		margin-top: 16rpx;
	}

	.xhx {
		padding: 24rpx 0  12rpx;
		border-bottom: 2rpx solid #e9e9e9;
	}
</style>
